const Router = require('koa-router')
const fuzzyquerycontent = new Router()
//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
  database: 'running',
  user: 'run',
  port: 5432,
  host: '82.157.94.115',
  password: 'z12345678',
  max: 2
})

fuzzyquerycontent.post('/fuzzyquerycontent', async ctx => {
  console.log(ctx.request.body);
  let {key} = ctx.request.body
  let searchContentPostgresql = `SELECT * FROM content WHERE content_id LIKE '${key}%'`;
  let content = await psql.query(searchContentPostgresql);
  if (content.rowCount > 0) {
    var fuzzyquerycontentObj=[];
    for (var i = 0; i < content.rowCount; i++) {
      var obj = {
        content_id: content.rows[i].content_id,
        content_title: content.rows[i].content_title,
        content_content: content.rows[i].content_content,
        content_time: content.rows[i].content_time,
        content_img: content.rows[i].content_img,
      }
      
      fuzzyquerycontentObj.push(obj);
    }

    ctx.body = {
        fuzzyquerycontentObj
    };
  }


})

module.exports = fuzzyquerycontent